package com.situ.skymall.admin.api.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.model.User;
import com.situ.model.UserAddress;
import com.situ.utils.JsonResult;
import com.situ.skymall.admin.api.service.UserFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.SpringQueryMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 用户控制器
 */
@RestController
@RequestMapping("/api/v1/user")
public class UserController {

    private final UserFeignService userFeignService;

    @Autowired
    public UserController(UserFeignService userFeignService) {
        this.userFeignService = userFeignService;
    }

    /**
     * 查询所有用户
     */
    @GetMapping("/findAll")
    public JsonResult<Page<User>> findAll(
            @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") int pageSize,
            @RequestParam(value = "username", required = false) String username,
            @RequestParam(value = "phone", required = false) String phone,
            @RequestParam(value = "roleType", required = false) Integer roleType) {

        return userFeignService.findAll(pageNum, pageSize, username, phone, roleType);
    }
    /**
     * 根据用户id查询用户
     */
    @GetMapping("/findById/{id}")
    public JsonResult<User> getUserById(@PathVariable Integer id) {
        return userFeignService.getUserById(id);
    }

    /**
     * 根据用户名查询用户
     */
    @GetMapping("/findByUsername/{username}")
    public JsonResult<User> getUserByUsername(@PathVariable String username) {
        return userFeignService.getUserByUsername(username);
    }

    /**
     * 修改用户状态
     */
    @PutMapping("/status/{id}/{status}")
    public JsonResult<String> updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        return userFeignService.updateStatus(id, status);
    }

    /**
     * 添加用户
     */
    @PostMapping("/add")
    public JsonResult<String> addUser(@RequestBody User user) {
        return userFeignService.addUser(user);
    }

    /**
     * 修改用户角色
     */
    @PutMapping("/role/{id}/{roleType}")
    public JsonResult<String> updateRoleType(@PathVariable Integer id, @PathVariable Integer roleType) {
        return userFeignService.updateRoleType(id, roleType);
    }

    /**
     * 用户信息更新
     */
    @PutMapping("/update")
    public JsonResult<String> updateUser(@RequestBody User user) {
        return userFeignService.updateUser(user);
    }

    /**
     * 用户信息删除
     */
    @DeleteMapping("/delete/{id}")
    public JsonResult<String> deleteUser(@PathVariable Integer id) {
        return userFeignService.deleteUser(id);
    }


    /**
     * 获取用户的所有地址
     */
    @GetMapping("/address/list/{userId}")
    public JsonResult<List<UserAddress>> getAddressListByUserId(@PathVariable Integer userId) {
        return userFeignService.getAddressListByUserId(userId);
    }

    /**
     * 获取用户的默认地址
     */
    @GetMapping("/address/default/{userId}")
    public JsonResult<UserAddress> getDefaultAddress(@PathVariable Integer userId) {
        return userFeignService.getDefaultAddress(userId);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("/address/default")
    public JsonResult<String> setDefaultAddress(@RequestParam Integer userId, @RequestParam Integer addressId) {
        return userFeignService.setDefaultAddress(userId, addressId);
    }

    /**
     * 新增用户地址
     */
    @PostMapping("/address/add")
    public JsonResult<String> addAddress(@RequestBody UserAddress userAddress) {
        return userFeignService.addAddress(userAddress);
    }

    /**
     * 修改用户地址
     */
    @PutMapping("/address/update")
    public JsonResult<String> updateAddress(@RequestBody UserAddress userAddress) {
        return userFeignService.updateAddress(userAddress);
    }

    /**
     * 删除用户地址
     */
    @DeleteMapping("/address/delete/{id}")
    public JsonResult<String> deleteAddress(@PathVariable Integer id) {
        return userFeignService.deleteAddress(id);
    }
}
